import BasicController from './../BasicController'
import {TeamModel} from './../../model'
import ErrorHandlerCode from './../../util/ErrorHandlerCode'

class TeamController extends BasicController{
	insert(req,res){
		if(!super.isEmptyParameter(req.body.team_title)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'team_title 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.team_description)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'team_description 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.cultivation_direction)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'cultivation_direction 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.leader_name)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'leader_name 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.leader_position)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'leader_position 为必填参数'
            })
            return;
    	}
        if(!super.isEmptyParameter(req.body.is_publish)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'is_publish 为必填参数'
            })
            return;
        }
    	TeamModel.insert(req.body).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
	}
	delTeam(req,res){
		if(!super.isEmptyParameter(req.body.id)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必填参数'
            })
            return;
    	}
    	TeamModel.deleteById(req.body.id).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
	}
	listpageTeam(req,res){
		if(!super.isEmptyParameter(req.body.page_size)){
    		req.body.page_size = 10;
    	}
    	if(!super.isEmptyParameter(req.body.page_no)){
    		req.body.page_no = 1;
    	}
    	TeamModel.findByPage(req.body).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
	}
	updateTeam(req,res){
		if(!super.isEmptyParameter(req.body.id)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必填参数'
            });
            return;
    	}
		TeamModel.update(req.body).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
	}
    findPublishList(req,res){
        if(!super.isEmptyParameter(req.body.is_publish)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'is_publish 为必填参数'
            });
            return;
        }
        TeamModel.findListPublish().then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    getTeamInfoById(req,res){
        if(!super.isEmptyParameter(req.body.id)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必填参数'
            });
            return;
        }
        TeamModel.findById(req.body.id).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
}

module.exports = new TeamController();